﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using Runda.CMS.Bll;
using Telerik.WebControls;
using Runda.CMS.Common.Entity;

namespace Runda.CMS.Portal.ContractManage
{
    public partial class SelectContractItemCalc : BasePage
    {
        public override bool IsCheckAccessible
        {
            get
            {
                return false;
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindContractSubItem();
            }
        }

        private void BindContractSubItem()
        {
            ContractBll bll = new ContractBll();
            DataTable dt = bll.GetContractItemTable(" and ParentID is not NULL and ContractID = '" + Request.QueryString["ContractID"] + "'", string.Empty);

            gridSubItems.DataSource = dt;
            gridSubItems.DataBind();

            CtrtContractEntity contractentity = bll.Get<CtrtContractEntity>(new Guid(Request.QueryString["ContractID"]));
            txtAverageProfitRate.Value = (double)contractentity.ContractProfitRate;
        }     

        protected void gridSubItems_ItemDataBound(object sender, Telerik.WebControls.GridItemEventArgs e)
        {
            if (e.Item.ItemType == Telerik.WebControls.GridItemType.Item || e.Item.ItemType == Telerik.WebControls.GridItemType.AlternatingItem)
            {
                DataRowView dv = e.Item.DataItem as DataRowView;
                CheckBox cbxSelect = e.Item.FindControl("cbxSelect") as CheckBox;
                Label lblRate = e.Item.FindControl("lblRate") as Label;
                if (dv != null)
                {
                    cbxSelect.Checked = Convert.ToBoolean(dv["IsCalcProfit"].ToString());
                    decimal Cost, PutDiscountPrice;
                    Cost = Convert.ToDecimal(dv["Cost"].ToString());
                    PutDiscountPrice = Convert.ToDecimal(dv["PutDiscountPrice"].ToString());
                    if (PutDiscountPrice != 0)
                    {
                        lblRate.Text = ((PutDiscountPrice - Cost) * 100 / PutDiscountPrice).ToString("N") + "%";
                    }
                }
            }
        }

        protected void gridSubItems_ItemCommand(object source, Telerik.WebControls.GridCommandEventArgs e)
        {

        }

        protected void cbxSelect_CheckedChanged(object sender, EventArgs e)
        {
            CheckBox cbxSelect = (CheckBox)sender;
            int index = ((GridDataItem)((cbxSelect ).NamingContainer)).ItemIndex;

            Guid ItemID = new Guid(gridSubItems.MasterTableView.DataKeyValues[index]["ItemID"].ToString());
            ContractBll bll = new ContractBll();
            CtrtContractItemEntity entity = bll.Get<CtrtContractItemEntity>(ItemID);
            entity.IsCalcProfit = cbxSelect.Checked;

            bll.Update(entity);
            BindContractSubItem();

            bll.CalculateContractProfitRate(new Guid(Request.QueryString["ContractID"]));
         
            CtrtContractEntity contractentity = bll.Get<CtrtContractEntity>(new Guid( Request.QueryString["ContractID"] ));
            txtAverageProfitRate.Value = (double)contractentity.ContractProfitRate; 
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            ContractBll bll = new ContractBll();
            CtrtContractEntity entity = bll.Get<CtrtContractEntity>(" and ContractID = '" + Request.QueryString["ContractID"] + "'");
            if (entity != null)
            {
                entity.ContractProfitRate = txtAverageProfitRate.Value.HasValue?(decimal)txtAverageProfitRate.Value.Value:0;
                bll.Update(entity);
                ClientScript.RegisterStartupScript(this.GetType(), "success", "alert('保存成功!');", true);
            }
        }
    }
}
